layui.define([ 'app', 'layer'], function (exports) {
    var app = layui.app;
    var layer = layui.layer;
    var index = {
        // 渲染左侧导航栏
        initLeftNav: function () {
            let param = {
                url: api.common.auth,
                tips: 0,
                callback: function (d) {
                    try{
                        let user_info = soda.session.getUser();
                        let avatar = user_info.avatar;
                        if(avatar == ''){
                            if(user_info.sex == 1){
                                avatar = '/assets/images/sex-1.png';
                            }else{
                                avatar = '/assets/images/sex-0.png';
                            }
                        }
                        user_info.avatar = avatar;
                        user_info.logo = '/assets/images/logo.png';
                        $('.layui-layout-admin .layui-header').vm(user_info);

                        Q.reg('console',function(){
                            app.showLoading('.layui-layout-admin .layui-body');
                            $('.layui-layout-admin .layui-body').load('/components/console.html', function () {
                                element.render('breadcrumb');
                                form.render();
                                app.removeLoading('.layui-layout-admin .layui-body');
                            });
                        });
                        element.render('nav');
                    }catch (e) {
                        // window.location.href = '/login.html';
                    }

                    var auth = d;
                    if(auth.length == 0){
                        soda.tips.error("当前尚未配置权限请联系管理员");
                        return false;
                    }
                    let vmAuth = $('.layui-layout-admin .layui-side').vm({menus: []});
                    vmAuth.set({menus: auth});
                    let menus = [];
                    $.each(auth, function (i, data) {
                        if (data.sub) {
                            $.each(data.sub, function (j, item) {
                                menus.push(item);
                                Q.reg(item.url,function(){
                                    app.showLoading('.layui-layout-admin .layui-body');
                                    $('.layui-layout-admin .layui-body').load(item.path, function () {
                                        element.render('breadcrumb');
                                        app.removeLoading('.layui-layout-admin .layui-body');
                                    });
                                });
                            })
                        }
                    });
                    soda.session.put(cache.menu, menus);
                    
                    let url = window.location.href;
                    if(url.indexOf('!') >= 0){
                        let item = url.split('!');
                        Q.init({
                            index: item[1]
                        });
                    }else{
                        Q.init({
                            index:'console'/* 首页地址 */
                        });
                    }
                    element.render('nav');
                }
            }
            soda.http.get(param);
        },
        // 页面元素绑定事件监听
        bindEvent: function () {
            // 退出登录
            $('#btnLogout').click(function () {
                layer.confirm('确定退出登录？', function (i) {
                    layer.close(i);
                    config.removeToken();
                    location.replace('login.html');
                });
            });
        }
    };
    exports('index', index);
});
